package com.tianzehua.nio;

import java.nio.IntBuffer;
import java.util.Random;

/**
 * @author tianzehua
 * @date 2019/03/26
 */
public class NioTest4 {
    public static void main(String[] args) {
        /* 生成随机数，然后将他们打印出来 */

        /*  分配一个10字节的缓存，里面只能放整数 */
        IntBuffer intBuffer = IntBuffer.allocate(10);
        System.out.println("init  limit :"+ intBuffer.limit());
        System.out.println("init position :"+ intBuffer.position());

        for (int i = 0; i < 5; ++i){
            int random = new Random().nextInt(20);
            intBuffer.put(random);
        }
        System.out.println("before flip limit :"+ intBuffer.limit());
        System.out.println("before flip position :"+ intBuffer.position());

        intBuffer.flip();

        System.out.println("after flip limit :"+ intBuffer.limit());
        System.out.println("after flip position :"+ intBuffer.position());

        while (intBuffer.hasRemaining()){
            System.out.println("limit :"+ intBuffer.limit());
            System.out.println("position :"+ intBuffer.position());
            System.out.println(intBuffer.get());
        }
    }
}
